package cn.cvs.servlet;
import cn.cvs.dao.MovieDao;
import cn.cvs.pojo.Movie;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet(urlPatterns = "/listServlet")
public class listServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        int status = 1;
        String statusstr = req.getParameter("status");
        if(statusstr!=null){
            status = Integer.parseInt(statusstr);
        }
        int size = 2;
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<Movie> list = sqlSession.getMapper(MovieDao.class).listmovie(status * size - size, size);
        //总记录数
        int count = sqlSession.getMapper(MovieDao.class).count();
        //总页数
        int totalcount = count/size;
        if((count%size)!=0){
            totalcount++;
        }
        req.getSession().setAttribute("status",status);
        req.getSession().setAttribute("list",list);
        req.getSession().setAttribute("count",count);
        req.getSession().setAttribute("totalcount",totalcount);
        resp.sendRedirect("listpage.jsp");
    }
}
